package com.pedro.streamer.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;


import com.pedro.streamer.R;
import com.pedro.streamer.utils.MediaUtil;
import com.pedro.streamer.utils.UrilToPath;

import java.util.List;
import java.util.Timer;

public class AIVideoAdapter extends RecyclerView.Adapter<AIVideoAdapter.TopTabViewHolder> {


    private static final String TAG = "com.bmit.engineermode";

    private Context mContext;
    private List<Uri> videoPicArr;

    private TopTabViewHolder lastView;
    private OnTextClickListener clickListener;
    private int time = 0;
    private Timer timer;

    @Override
    public TopTabViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ai_video_recycle_layout, parent, false);
        mContext = parent.getContext();

        return new TopTabViewHolder(view);
    }

    static class TopTabViewHolder extends RecyclerView.ViewHolder {


        ImageButton deleteText;
        ImageView imageView;
        TextView addText;
        ConstraintLayout videoImgLayout,chooseLayout;


        public TopTabViewHolder(View view) {
            //父类构造函数
            super(view);

            videoImgLayout = view.findViewById(R.id.video_image_layout);
            chooseLayout = view.findViewById(R.id.choose_video_image_layout);

            imageView = videoImgLayout.findViewById(R.id.image_view);
            deleteText = videoImgLayout.findViewById(R.id.delete_text);

            addText = chooseLayout.findViewById(R.id.add_video_text);


        }

    }

    @Override
    public void onBindViewHolder(@NonNull TopTabViewHolder holder, int position) {

        if (position < videoPicArr.size()) {
            holder.videoImgLayout.setVisibility(View.VISIBLE);
            holder.chooseLayout.setVisibility(View.GONE);

            Uri videoPath = videoPicArr.get(position);
            if (videoPath != null){
                holder.imageView.setImageBitmap(MediaUtil.getVideoFirstFrame(UrilToPath.getImageAbsolutePath(mContext, videoPath)));
            }

            holder.deleteText.setVisibility(View.GONE);

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (lastView != null) {
                        lastView.deleteText.setVisibility(View.GONE);
                    }
                    lastView = holder;
                    lastView.deleteText.setVisibility(View.VISIBLE);
                    if (clickListener != null) {
                        clickListener.onTextClick(videoPath);
                    }

                }
            });

            holder.deleteText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (clickListener != null) {
                        if (lastView != null) {
                            lastView = null;
                        }
                        clickListener.onDeleteClick(videoPath);
                    }
                }
            });



        }else {

            holder.videoImgLayout.setVisibility(View.GONE);
            holder.chooseLayout.setVisibility(View.VISIBLE);
            holder.chooseLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (clickListener != null) {
                        clickListener.addVideo();
                    }
                }
            });

        }






    }


    public void setClickListener(OnTextClickListener clickListener) {
        this.clickListener = clickListener;
    }


    public void setVideoPicArr(List<Uri> videoPicArr) {
        this.videoPicArr = videoPicArr;
    }

    @Override
    public int getItemCount() {

        return videoPicArr.size() + 1;
    }

    public interface OnTextClickListener {
        void onTextClick(Uri path);

        void onDeleteClick(Uri path);
        void addVideo();
    }


}
